#!/bin/bash

if [ $# -lt 2 ]; then
  echo  "usage: ren srcfile destname extname"
  echo  "usage: ren srcfile destname \"(null)\""
  exit 1 
fi
if [ -e ${*:$[$#-1]:1} ]; then
 echo "error: destname can't be file!"
 exit 2
fi
if [ -e ${*:$#:1} ]; then
 echo "error: extname can't be file!"
 exit 3
fi
for((i=1;  $i < $[$#-1]; i=$[$i+1])); do
  if [ -e ${*:$i:1} ] && ! [ -d ${*:$i:1} ]; then
    if [ ${*:$#:1} = "(null)" ]; then
      mv ${*:$i:1} ${*:$[$#-1]:1}$i 
    else
      mv ${*:$i:1} ${*:$[$#-1]:1}$i.${*:$#:1}
   fi
  fi
done
